package www.dianbaozu.com.ui.server

import android.text.InputFilter
import android.view.View
import androidx.lifecycle.lifecycleScope
import www.dianbaozu.com.R
import www.dianbaozu.com.extension.requestUniversally
import www.wen.com.common.lib.data.entities.RequestBodyParameter
import www.wen.com.common.lib.data.entities.ServerUpdateInfoEntity
import www.wen.com.common.lib.data.entities.ViewPagePosition
import www.wen.com.common.lib.extension.popProxy
import www.wen.com.common.lib.extension.postObject
import www.wen.com.common.lib.extension.toast
import www.wen.com.common.lib.extension.trimProxy
import www.wen.com.common.lib.helper.loading.KeyHelper


/**
 * @author WEN
 * @Description:创建新分组
 * @date 2020//25 17:48
 */
class CreateGroupInServerFragment : ChangingUseNameInServerFragment(),
        View.OnClickListener {

    override fun getName(): CharSequence = ""

    override fun getTitleName() = getString(R.string.create_group)

    override fun geRightText() = getString(R.string.create_channel_enter)

    override fun getCreateGroupText(): CharSequence = getString(R.string.group_name)

    override fun getCreateGroupText1(): CharSequence = getString(R.string.input_group_name)

    override fun getCheckText(): String = getString(R.string.com_group_name_cannot_be_empty)

    override fun lazyInitView() {
        super.lazyInitView()
        dataBinding.etPleaseEnterAGroupName.filters = arrayOf<InputFilter>(InputFilter.LengthFilter(16))
    }

    override fun actionRight() {
        if (dataBinding.etPleaseEnterAGroupName.trimProxy().isBlank()) {
            toast(getCheckText())
            return
        }
        mUpdateServerInfoViewModel.requestUniversally(
                viewModelScope = lifecycleScope,
                tipSuccess = getString(R.string.create_group_success),
                isShowToastTip = true,
                resultFailCall = {
                    mUpdateServerInfoViewModel.mIsShowErrorTip.set(View.VISIBLE)
                    mUpdateServerInfoViewModel.mErrorTipText.set(it)
                },
                request = { appDataManage ->
                    appDataManage.requestCreateNewGroup(
                            RequestBodyParameter(
                                    serverId = mServerInfo.serverId,
                                    groupName = dataBinding.etPleaseEnterAGroupName.trimProxy()
                            ))
                }
        ) {
            //选中对应的服务器并展示
            postObject(KeyHelper.REFRESHING_SERVER_LIST_KEY3,
                    ServerUpdateInfoEntity(
                            serverId = mServerInfo.serverId,
                            serverName = mServerInfo.serverName))
            //主页切换服务器页面
            postObject(KeyHelper.MAIN_VIEWPAGER_POSITION_KEY, ViewPagePosition(0))
            //返回主页
            popProxy(R.id.mainFragment)
        }
    }

}



